package cola.project.hellojavaee.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@WebServlet(name = "TimeServlet" ,value = "/time-servlet")
public class TimeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String date = dateFormat.format(new Date());
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write(date);

/*        // 拿到 ServletContext 中的数据
        ServletContext servletContext = getServletContext();
        Object cola = servletContext.getAttribute("cola");
        System.out.println(cola.toString());*/
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("这是在 TimeServlet 中的数据：");
        // 由 forwardServlet 转发来的 post
        req.getParameterMap().forEach((k,v)->{
            System.out.println(k+": "+ Arrays.toString(v));
        });
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}
